/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package aso.upna.cancion;

import entagged.audioformats.AudioFile;
import entagged.audioformats.AudioFileIO;
import entagged.audioformats.exceptions.CannotReadException;
import entagged.audioformats.exceptions.CannotWriteException;
import java.io.File;

/**
 *
 * @author javier
 */
public class Metadata {
    public final int TITULO = 0;
    public final int GRUPO = 1;
    public final int ALBUM = 2;
    private final int N_TAGS = 3;
    private String filename;
    private boolean hasMetadata;
    private String[] tags;

    public Metadata(String filename)
    {
        this.filename = filename;
        tags = new String[N_TAGS];
        loadMetadata(filename);
        
    }

    private void loadMetadata(String filename)
    {
        try
        {
            File curr_file = new File(filename);
            AudioFile audioFile = AudioFileIO.read(curr_file);

            if (audioFile == null)
            {
                return;
            }

            if(audioFile.getTag() == null)
            {
                hasMetadata = false;
                return;
            }
            else
                hasMetadata = true;

            if(audioFile.getTag().getFirstTitle()==null)
                setTitulo("Desconocido");
            else
                tags[TITULO] = audioFile.getTag().getFirstTitle();

            if(audioFile.getTag().getFirstArtist()==null)
                setGrupo("Desconocido");
            else
                tags[GRUPO] = audioFile.getTag().getFirstArtist();

            if(audioFile.getTag().getFirstAlbum()==null)
                setAlbum("Desconocido");
            else
                tags[ALBUM] = audioFile.getTag().getFirstAlbum();
            
            tags[TITULO] = audioFile.getTag().getFirstTitle();
            tags[GRUPO] = audioFile.getTag().getFirstArtist();
            tags[ALBUM] = audioFile.getTag().getFirstAlbum();

        } catch (CannotReadException ex) {
            System.out.println("-------> PUTIIIIIIIIIIII <-----------");
            ex.printStackTrace();
        }
    }

    public void sync()
    {
        try
        {
            File curr_file = new File(filename);
            AudioFile audioFile = AudioFileIO.read(curr_file);
            audioFile.getTag().setTitle(tags[TITULO]);
            audioFile.getTag().setArtist(tags[GRUPO]);
            audioFile.getTag().setAlbum(tags[ALBUM]);
            audioFile.commit();
        }
        catch (CannotWriteException ex)
        {
            ex.printStackTrace();
        }
        catch (CannotReadException ex)
        {
            ex.printStackTrace();
        }
    }

    public boolean hasMetadata()
    {
        return hasMetadata;
    }

    public String getTitulo()
    {
        return tags[TITULO];
    }

    public String getGrupo()
    {
        return tags[GRUPO];
    }

    public String getAlbum()
    {
        return tags[ALBUM];
    }

    public void setTitulo(String titulo)
    {
        tags[TITULO] = new String(titulo);
    }

    public void setGrupo(String grupo)
    {
        tags[GRUPO] = new String(grupo);
    }

    public void setAlbum(String album)
    {
        tags[ALBUM] = new String(album);
    }
}
